package com.tarena.lbs.basic.web.dao.repository.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.dao.mapper.StoreMapper;
import com.tarena.lbs.basic.web.dao.repository.StoreRespository;
import com.tarena.lbs.pojo.basic.bo.StoreBO;
import com.tarena.lbs.pojo.basic.dos.StoreDO;
import com.tarena.lbs.pojo.basic.query.AreaStoreQuery;
import com.tarena.lbs.pojo.basic.query.StoreQuery;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Repository
public class StoreRepositoryImpl implements StoreRespository {
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private RestHighLevelClient client;

    @Override
    public BasePageList<StoreBO> getStoreList(StoreQuery query) {
        log.info("店铺分页数据,pageNo:{},pageSize:{}",query.getPageNo(),query.getPageSize());
        //1.开启PageHelper查询
        PageHelper.startPage(query.getPageNo(),query.getPageSize());
        //2. select sql
        List<StoreDO> dos = storeMapper.selectStoresByPage(query);
        //3.DO to BO
        if (CollectionUtils.isNotEmpty(dos)){
            PageInfo<StoreDO> pageDos = new PageInfo<>(dos);
            List<StoreBO> bos = pageDos.getList().stream().map(storeDO -> {
                StoreBO bo = new StoreBO();
                BeanUtils.copyProperties(storeDO, bo);
                return bo;
            }).collect(Collectors.toList());
            return new BasePageList<>(bos, pageDos.getTotal());
        }
        return new BasePageList<>(null,0L);
    }

    @Override
    public Integer saveStore(StoreDO storeDO) {
        //1.写入数据库.获取Id
       Integer id= storeMapper.insertDb(storeDO);
       log.debug("存储Mybatis数据库返回的id:{}",id);
       //更改 StoreDO的属性值
       storeDO.setId(id);

       //2.写入ES数据库
        insertES(storeDO);
        return id;
    }

    @Override
    public BasePageList<StoreBO> getStoresByAreasAndBusiness(Integer id, AreaStoreQuery query) {
        PageHelper.startPage(query.getPageNo(),query.getPageSize());
        List<StoreDO> dos=storeMapper.selectStoresByAreasAndBusiness(id,query);
        log.debug("查询数据库信息:dos:{}",dos);
        if (dos!=null){
            PageInfo<StoreDO> pageDos=new PageInfo<>(dos);
            List<StoreBO> bos=pageDos.getList().stream().map(storeDO->{
                StoreBO bo=new StoreBO();
                BeanUtils.copyProperties(storeDO,bo);
                log.debug("StoreBD =:{}",bo);
                return bo;
            }).collect(Collectors.toList());
            return new BasePageList<>(bos,pageDos.getTotal());
        }
        return new BasePageList<>(null,0L);
    }

    private void insertES(StoreDO storeDO) {
        //INSERT ES
        IndexRequest indexRequest = new IndexRequest("lbs_store");
        //指定索引,转化JSON写入DOC
        indexRequest.id(String.valueOf(storeDO.getId()));
        //将store转化为JSON
        String sourceStore = JSON.toJSONString(storeDO);
        log.info("当前新增店铺:{}",sourceStore);
        indexRequest.source(sourceStore, XContentType.JSON);
        try {
            client.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("新增文档异常",e);
        }
    }
}
